//
//  HJMainViewController.m
//  MJReset_Test
//
//  Created by Host on 16/3/20.
//  Copyright © 2016年 Host. All rights reserved.
//

#import "HJMainViewController.h"
#import "HJLeftViewController.h"
#import "HJRequestNet.h"
#import "HJHomePageItemModel.h"
#import <MJRefresh.h>

@interface HJMainViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray * _allData;
    UITableView * _tableView;
    NSInteger _page;
}
@end

@implementation HJMainViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [_tableView.mj_header beginRefreshing];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"主页面";
    UIBarButtonItem * leftBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"Left" style:UIBarButtonItemStylePlain target:nil action:@selector(enterLeftNavication)];
    //    UIBarButtonItem *leftBarBtn= [[UIBarButtonItem alloc]initWithTitle:@"左导航" style:UIBarButtonItemStyleBordered target:self action:@selector(enterLeftNavication)];
    self.navigationItem.leftBarButtonItem = leftBarBtn;
    self.view.backgroundColor = [UIColor greenColor];
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
//    [self upDataRequest];
    
    
}

- (void)upDataRequest {
    
    NSString * str = @"http://zhekou.repai.com/lws/wangyu/index.php?control=babynews&model=index3&app_id=1066136336&app_oid=fa561552f3235cd0338f6ad2a1bf76a91cfe65c5&app_version=3.0&app_channel=appstore&cid=0";
    __weak HJMainViewController * viewC = self;
    [HJRequestNet getRequestWithURLStr:str WithDataBlock:^(NSDictionary *allData) {
        [viewC endRefresh];
        
        _allData = [HJHomePageItemModelData managerWith:allData];
        [viewC allData];
        [viewC reloadDtatee];
    } isError:^(NSError *error) {
        
    }];

}

- (NSMutableArray* )allData {
    return _allData;
}


- (void)reloadDtatee{
    [_tableView reloadData];
}

- (void)endRefresh {
    [_tableView.mj_header endRefreshing];
    [_tableView.mj_footer endRefreshing];
}


- (void)enterLeftNavication {
    
    HJLeftViewController * left = [[HJLeftViewController alloc] init];
    UINavigationController *leftNavVC = [[UINavigationController alloc]initWithRootViewController:left];
    [self.navigationController pushViewController:leftNavVC animated:YES];
    
//    [self.revealSideViewController pushViewController:leftNavVC onDirection:PPRevealSideDirectionLeft animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _allData.count;
}

- (UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    HJHomePageItemModel * model = [_allData objectAtIndex:indexPath.row];
    cell.textLabel.text = model.title;
    return cell;
    
}
    
@end
